import Loading from '../components/myLoading.vue'

export default {
  install(Vue, pluginOptions = {}) {
    const VueLoading = Vue.extend(Loading)
    let loading = null

    function $loading(params) {
      return new Promise((resolve) => {
        if (!loading) {
          loading = new VueLoading()
          // 手动创建一个未挂载的实例
          loading.$mount()
          // 挂载
          document.querySelector(pluginOptions.container || 'body').appendChild(loading.$el)
        }
        // 显示loading
        loading.show(params)
        resolve()
      })
    }
    // 定义关闭loading方法
    $loading.end = () => {
      return new Promise((resolve) => {
        if (!loading || !loading.isShow) {
          resolve()
          return
        }
        loading.hide()
      })
    }

    Vue.loading = Vue.prototype.$loading = $loading
  }
}
